package com.unidt.www.module.system.dal.mysql.dept;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.unidt.www.framework.mybatis.core.mapper.BaseMapperX;
import com.unidt.www.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.unidt.www.module.system.dal.dataobject.dept.UserPostDO;
import com.unidt.www.module.system.dal.dataobject.permission.UserRoleDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.*;

@Mapper
public interface UserPostMapper extends BaseMapperX<UserPostDO> {

    default List<UserPostDO> selectListByUserId(Long userId) {
        return selectList(new LambdaQueryWrapperX<UserPostDO>()
                .eq(UserPostDO::getUserId, userId));
    }

    default void deleteByUserIdAndPostId(Long userId, Collection<Long> postIds) {
        delete(new LambdaQueryWrapperX<UserPostDO>()
                .eq(UserPostDO::getUserId, userId)
                .in(UserPostDO::getPostId, postIds));
    }

    default List<UserPostDO> selectListByPostIds(Collection<Long> postIds) {
        return selectList(new LambdaQueryWrapperX<UserPostDO>()
                .in(UserPostDO::getPostId, postIds));
    }

    default void deleteByUserId(Long userId){
        delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId));
    }


    default int selectCountByPost(Collection<Long> postIds){
        List<UserPostDO> mlist=selectList(new QueryWrapper<UserPostDO>()
                .in("post_id", postIds));
        Set<UserPostDO> set = new HashSet<>(mlist);
        /**
         * 去重后的数组
         */
        List<UserPostDO> mlistSet=new ArrayList<>(set);

        return mlistSet.size();
    }

}
